package com.xyxeshang.es.music.backend.util;


import com.xyxeshang.es.music.backend.constant.ResultConstant;
import com.xyxeshang.es.music.backend.exception.CustomException;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.transaction.NoTransactionException;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.Map;


/**
 * @author xyx-Eshang
 */
public class ResultUtil<T> {
	@ApiModelProperty(value = "状态码", example = "200")
	private Integer code;

	@ApiModelProperty(value = "提示信息", example = "成功")
	private String message;

	@ApiModelProperty(value = "返回数据")
	private Map<String, T> data;

	/**
	 * 01 空参构造：成功
	 */
	public ResultUtil() {
		this.code = ResultConstant.SUCCESS;
		this.message = ResultConstant.messageMap.get(code);
	}

	/**
	 * 02. 只有状态码的构造方法
	 *
	 * @param code
	 */
	public ResultUtil(Integer code) {
		this.code = code;
		this.message = ResultConstant.messageMap.get(code);
	}

	/**
	 * 03. 有key、有value的构造方法
	 *
	 * @param dataKey
	 * @param dataValue
	 */
	public ResultUtil(String dataKey, T dataValue) {
		this.code = ResultConstant.SUCCESS;
		this.message = ResultConstant.messageMap.get(code);
		Map<String, T> dataMap = new HashMap<>();
		dataMap.put(dataKey, dataValue);
		this.data = dataMap;
	}

	/**
	 * 04. 有code，有key，有value的构造方法
	 *
	 * @param code
	 * @param dataKey
	 * @param dataValue
	 */
	public ResultUtil(Integer code, String dataKey, T dataValue) {
		this.code = code;
		this.message = ResultConstant.messageMap.get(code);
		Map<String, T> dataMap = new HashMap<>();
		dataMap.put(dataKey, dataValue);
		this.data = dataMap;
	}

	/**
	 * 05. 异常时：回滚
	 *
	 * @param exception
	 */
	public ResultUtil(Exception exception) {
		try {
			//手动回滚
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} catch (NoTransactionException e) {
			//如果方法没有开启事务，那么这里会抛出异常。不处理即可
		}
		if (exception.getClass().getSimpleName().contentEquals(CustomException.class.getSimpleName())) {
			CustomException e = (CustomException) exception;
			this.code = e.getCode();
			this.message = e.getMessage();
			System.err.println("<===发生自定义异常===>\n" + this.message);
		} else {
			exception.printStackTrace();
			this.code = 40000;
			this.message = "系统异常，异常信息如下：\n" + exception.getMessage();
		}
		this.data = null;
	}

	@Override
	public String toString() {
		return "●\t该方法的返回值结果如下：\n" +
				"●\tcode：" + code + '\n' +
				"●\tmessage：" + message + '\n' +
				"●\tdata：\n" + data;
	}

	public Integer getCode() {
		return code;
	}

	public ResultUtil<T> setCode(Integer code) {
		this.code = code;
		return this;
	}

	public String getMessage() {
		return message;
	}

	public ResultUtil<T> setMessage(String message) {
		this.message = message;
		return this;
	}

	public Map<String, T> getData() {
		return data;
	}

	public ResultUtil<T> setData(Map<String, T> data) {
		this.data = data;
		return this;
	}

}